/**
 * 
 * @author Sunix
 * @date   2013-5-20
 * @Email  SunixSky@gmail.com
 * 
 */	
package view.components{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.utils.StringUtil;
	
	import manager.CategoryManager;
	import manager.DialogueManager;
	import manager.LoggerManager;
	import manager.PanelManager;
	
	import scaleform.clik.controls.Button;
	import scaleform.clik.controls.DropdownMenu;
	import scaleform.clik.controls.TextInput;
	import scaleform.clik.data.DataProvider;

	public class CategoryOperator extends Sprite{
		
		public var ctegoryList:DropdownMenu;   //下拉条
		public var type:TextInput;          //类型
		public var prefix:TextInput;        //前缀
		public var startNum:TextInput;      //起始数值
		public var addBtn:Button;           //增加按钮
		public var delBtn:Button;           //删除按钮
		public var editBtn:Button;          //保存按钮
		public var backBtn:Button;          //返回按钮
		
		protected var data:Object;          //当前选择对象数据
		protected var dataProvider:DataProvider;  //数据适配器
		
		public function CategoryOperator(){
			addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
		}
		
		protected function onAddedToStage(event:Event):void{
			viewInit();
			eventInit();
		}
		
		private function viewInit():void{
			updateCtegoryList();
			x= 311;
			startNum.textField.restrict = "0-9";
		}
		
		private function eventInit():void{
			backBtn.addEventListener(MouseEvent.CLICK,onBack);
			addBtn.addEventListener(MouseEvent.CLICK,onAdd);
			delBtn.addEventListener(MouseEvent.CLICK,onDel);
			editBtn.addEventListener(MouseEvent.CLICK,onEdit);
			ctegoryList.addEventListener(Event.CHANGE,onChange);
		}
		
		protected function onDel(event:MouseEvent):void{
			if(!data){
				LoggerManager.error("数据不存在");
				return;
			}
			DialogueManager.showDialogue('是否删除类别【' + type.text + '】？注意，该操作无法还原！',null,deleteXml);
		}
		
		protected function deleteXml():void{
			CategoryManager.deleteXml(data.key);
			updateCtegoryList();
			LoggerManager.log("删除类别信息成功");
		}
		
		protected function onEdit(event:MouseEvent):void{
			if(!validCheck())
				return;
			DialogueManager.showDialogue('是否编辑类别【' + type.text　+ '】？注意，该操作无法还原！',null,editXml);
		}
		
		protected function editXml():void{
			var index:uint = data.key;
			var key:String = index.toString();
			var xml:XML = getNewXml(key);
			CategoryManager.editXml(xml,key);
			updateCtegoryList();
			LoggerManager.log("编辑类别信息成功");
		}
		
		protected function onAdd(event:MouseEvent):void{
			if(!validCheck())
				return;
			DialogueManager.showDialogue('是否添加新类别【'+ type.text +'】？',null,addXml);
		}

		protected function addXml():void{
			var index:uint = uint(CategoryManager.xml.@key) + 1;
			var key:String = index.toString();
			var xml:XML = getNewXml(key);
			CategoryManager.addXml(xml,key);
			updateCtegoryList();
			LoggerManager.log("新增类别信息成功");
		}
		
		protected function getNewXml(key:String):XML{
			var typeName:String = StringUtil.trim(type.text);
			var prefixName:String = StringUtil.trim(prefix.text);
			var numName:String = StringUtil.trim(startNum.text);
			var xml:XML=<info type={typeName} prefix={prefixName} num={numName} key={key} />;
			return xml;
		}
		
		protected function onChange(event:Event = null):void{
			data = ctegoryList.dataProvider[ctegoryList.selectedIndex];
			if(!data){
				type.text = '';
				prefix.text = '';
				startNum.text = '';
				return;
			}
			type.text = data.label;
			prefix.text = data.prefix;
			var value:uint = uint(data.num);
			startNum.text = value.toString();
		}
		
		protected function validCheck():Boolean{
			if(!StringUtil.trim(type.text)){
				LoggerManager.error("请输入类别名称");
				return false;
			}
			if(!StringUtil.trim(prefix.text)){
				LoggerManager.error("请输入类别前缀");
				return false;
			}
			if(!StringUtil.trim(startNum.text)){
				LoggerManager.error("请输入起始数字");
				return false;
			}
			return true;
		}
		
		protected function onBack(event:MouseEvent):void{
			PanelManager.showAnimationWindow();
		}
		
		protected function updateCtegoryList():void{
			dataProvider = new DataProvider(CategoryManager.categoryArray);
			ctegoryList.dataProvider = dataProvider;
			ctegoryList.selectedIndex = 0;
		}
	}
}